<?php
/**
 * @package Content - Related News
 * @version 1.7.0
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @copyright (c) 2012 YouTech Company. All Rights Reserved.
 * @author YouTech Company http://www.smartaddons.com
 *
 */
defined('_JEXEC') or die;

class plgContentRelatedNews extends JPlugin {
	
	/**
	 * Plugin that loads content within content
	 *
	 * @param	string	The context of the content being passed to the plugin.
	 * @param	object	The article object.  Note $article->text is also available
	 * @param	object	The article params
	 * @param	int		The 'page' number
	 */
	public function onContentPrepare($context, &$article, &$params, $page=0) {
		// simple performance check to determine whether bot should process further
		$depends = $this->params->get('depends');
		if (!is_array($depends) || count($depends)==0 || !isset($article->catid) || !in_array($article->catid, $depends)){
			return;
		}
		if ($context=='com_content.article'){
			$this->getTools();
			
			$article_id = $article->id;
			$category_id = $article->catid;
			
			$content = &$this->getContentReader();
			$items = $content->getCategoryItems($category_id, $this->params->toArray());
			
			if (isset($items[$article_id])){
				unset($items[$article_id]);
			}
			
			if (count($items)==0){
				return;
			} else {
				foreach ($items as $id => $item) {
					$content->getItemUrl($item);
					if (!$content->getItemImage($item)){
						$image_url = JURI::root() . 'plugins/content/relatednews/assets/images/nophoto.gif';
						$item->image = $image_url;
					}
				}
			}
			
			$patterns = array();
			$regex = "#{relatednews\s?(.*?)}#s";
			preg_match_all($regex, $article->text, $matches);
			if (isset($matches[0])&&count($matches[0])){
				foreach ($matches[0] as $i => $match) {
					$key = trim($matches[0][$i]);
					$val = trim($matches[1][$i]);
					$patterns[$key] = $val;
				}
			}
			
			$_load_templates = array();
			if (count($patterns)){
				foreach ($patterns as $replace => $_params) {
					$_params = $this->_parseParams($_params);
					if (isset($_params['template'])){
						$_load_templates[$replace] = $this->_getLayoutPath($_params['template']);
					} else {
						$_load_templates[$replace] = $this->_getLayoutPath();
					}
				}
			} else {
				$_load_templates[] = $this->_getLayoutPath();
			}
			
			foreach ($_load_templates as $search => $layout){
				@ob_start();
				include $layout;
				$replace = ob_get_contents();
				@ob_end_clean();
				if (is_string($search)){
					//$search = preg_quote($search);
					$article->text = preg_replace("#$search#s", $replace, $article->text, 1);
				} else {
					$article->text .= $replace;
				}
				
			}
		}
	}
	
	protected function getTools(){
		if (!class_exists('SjTools')){
			include_once dirname(__FILE__).DS.'core'.DS.'classes'.DS.'sjtools.php';
		}
	}
	
	protected function getContentReader(){
		if(!class_exists('sjContentReader')){
			include_once dirname(__FILE__).DS.'core'.DS.'classes'.DS.'content.php';
		}
		if (!isset($this->reader)){
			$this->reader = new _ContentReader();
		}
		return $this->reader;
	}
	
	private function _parseParams($_params){
		$array = array();
		if (!empty($_params)){
			$parts = explode(' ', $_params);
			if (count($parts)){
				foreach ($parts as $part){
					list($key, $val) = explode('=', $part);
					if (isset($val)){
						$key = trim($key);
						$val = htmlspecialchars_decode($val);
						$val = trim($val);
						$val = substr($val, 1, strlen($val)-2);
						
						$array[$key] = $val;
					}
				}
			}
			if (count($array)){
				foreach ($array as $param => $value){
					$this->params->set($param, $value);
				}
			}
		}
		return $array;
	}
	
	private function _getLayoutPath($tpl='default'){
		$filename = $tpl . '.php';
		$dirname  = dirname(__FILE__) . DS . 'tmpl';
		if (file_exists($dirname.DS.$filename)){
			return $dirname.DS.$filename;
		}
		return $dirname.DS.'default.php';
	}
	
	
}
